﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static LiWi.Shell;
namespace LiWi
{
    public partial class 直线插补 : Form
    {
        CancellationTokenSource cts = new CancellationTokenSource();


        #region
        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        public bool axisFlag = false;             //轴状态
        public int[] axisList = { 0, 1, 2, 3 };


        public int runmode;                     //1：相对/绝对
        public int movemode;                    //1：2点直线/3点直线/4点/

        public double _speed;                   //速度
        public double _accel;                   //加速度
        public double _decel;                   //减速度

        public double _destPos1;                //终点位置1
        public double _destPos2;                //终点位置2
        public double _destPos3;                //终点位置3
        public double _destPos4;                //终点位置4
        public WindowsAdapt adapt = new WindowsAdapt();

        //public double _midPos1;                 //圆弧中点1
        //public double _midPos2;                 //圆弧中点2
        //public double _midPos3;                 //圆弧中点3
        //public double _midPos4;                 //圆弧中点4

        #endregion

        public 直线插补()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            initValue();
            adapt.SetForm(this);
        }





        private void Form1_Load(object sender, EventArgs e)
        {


        }
        public void initValue()
        {
            //TODO

            destpos0.Text = "0";
            destpos1.Text = "0";
            destpos2.Text = "0";
            destpos3.Text = "0";

            if (radrunMove1.Checked) runmode = 1;
            else if (radrunMove2.Checked) runmode = 2;
            if (radMoveMode1.Checked) movemode = 1;
            else if (radMoveMode2.Checked) movemode = 2;
            else if (radMoveMode3.Checked) movemode = 3;



        }





        private void radrunMove1_CheckedChanged(object sender, EventArgs e)
        {
            runmode = 1;
        }

        private void radrunMove2_CheckedChanged(object sender, EventArgs e)
        {
            runmode = 2;
        }

        private void radMoveMode1_CheckedChanged(object sender, EventArgs e)
        {
            movemode = 1;
        }

        private void radMoveMode2_CheckedChanged(object sender, EventArgs e)
        {
            movemode = 2;
        }

        private void radMoveMode3_CheckedChanged(object sender, EventArgs e)
        {
            movemode = 3;
        }

        private void radMoveMode4_CheckedChanged(object sender, EventArgs e)
        {
            movemode = 4;
        }

        /// <summary>
        /// 坐标清零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btnzero_Click(object sender, EventArgs e)
        {
            foreach (var item in axisList)
            {
                PMC_SetDpos(_handle, item, 0);
            }
        }




        public async void axisStart()
        {
            try
            {
                var ret = 0;
                axisFlag = true;
                Btnstart.Enabled = false;
                Btnstart.Text = "运动中";
                float[] destlist = { Convert.ToSingle(destpos0.Text), Convert.ToSingle(destpos1.Text), Convert.ToSingle(destpos2.Text), Convert.ToSingle(destpos3.Text) };
                //判断缓冲剩余量:如果缓冲小于20等待一下
                await Task.Run(() =>
                {
                    while (Data.ReMainBuffer(axisList) <= 20)
                    {
                        Thread.Sleep(200);
                    }
                    if (runmode == 1)           //相对
                    {
                        switch (movemode)
                        {

                            case 1:
                                ret = PMC_Move(_handle, 2, axisList, destlist);
                                if (ret != 0)
                                {
                                    MessageBox.Show("启动失败");
                                    Btnstart.Enabled = true;
                                    Btnstart.Text = "启 动";
                                    axisFlag = false;
                                    return;
                                }
                                break;
                            case 2:
                                ret = PMC_Move(_handle, 3, axisList, destlist);
                                if (ret != 0)
                                {
                                    MessageBox.Show("启动失败");
                                    Btnstart.Enabled = true;
                                    Btnstart.Text = "启 动";
                                    axisFlag = false;
                                    return;
                                }
                                break;
                            case 3:
                                ret = PMC_Move(_handle, 4, axisList, destlist);
                                if (ret != 0)
                                {
                                    MessageBox.Show("启动失败");
                                    Btnstart.Enabled = true;
                                    Btnstart.Text = "启 动";
                                    axisFlag = false;
                                    return;
                                }
                                break;
                        }
                    }
                    else if (runmode == 2)      //绝对
                    {
                        switch (movemode)
                        {
                            case 1:
                                ret = PMC_MoveAbs(_handle, 2, axisList, destlist);
                                if (ret != 0)
                                {
                                    MessageBox.Show("启动失败");
                                    Btnstart.Enabled = true;
                                    Btnstart.Text = "启 动";
                                    axisFlag = false;
                                    return;
                                }
                                break;
                                break;
                            case 2:
                                ret = PMC_MoveAbs(_handle, 3, axisList, destlist);
                                if (ret != 0)
                                {
                                    MessageBox.Show("启动失败");
                                    Btnstart.Enabled = true;
                                    Btnstart.Text = "启 动";
                                    axisFlag = false;
                                    return;
                                }
                                break;
                                break;
                            case 3:
                                ret = PMC_MoveAbs(_handle, 4, axisList, destlist);
                                if (ret != 0)
                                {
                                    MessageBox.Show("启动失败");
                                    Btnstart.Enabled = true;
                                    Btnstart.Text = "启 动";
                                    axisFlag = false;
                                    return;
                                }
                                break;
                                break;
                        }
                    }
                    Thread thsport = new Thread(() =>
                    {
                        foreach (var item in axisList)
                        {
                            var a = 0;
                            PMC_GetIfIdle(_handle, item, ref a);
                            while (a == 0)
                            {
                                PMC_GetIfIdle(_handle, item, ref a);
                                Thread.Sleep(50);
                            }
                        }
                        axisFlag = false;
                        Btnstart.Enabled = true;
                        Btnstart.Text = "启 动";


                    }
                    );
                    thsport.Start();
                });


            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                Btnstart.Enabled = true;
                Btnstart.Text = "启 动";
                axisFlag = false;
            }
        }



        private void Btnstart_Click(object sender, EventArgs e)
        {
            PMC_Rapidstop(_handle, 3);
            for (int i = 0; i < 4; ++i)
            {
                int enable = 0;
                PMC_GetAxisEnable(_handle, i, ref enable);
                if (enable == 0)
                {
                    MessageBox.Show("请回连接处将4个轴使能！！！");
                    return;
                }
                PMC_SetSpeed(_handle, i, Convert.ToInt32(num_sp.Value));
                PMC_SetUnits(Data.handle, i, Convert.ToInt32(num_uint.Value));
                PMC_SetAccel(Data.handle, i, Convert.ToInt32(num_accel.Value));
                PMC_SetDecel(Data.handle, i, Convert.ToInt32(num_decel.Value));
            }
            Data._pause = false;
            if (isConnect) axisStart();
            else MessageBox.Show("请先连接控制器！！！");

        }

        private void Move_FormClosing(object sender, FormClosingEventArgs e)
        {
            cts.Cancel();
        }

        private void BtnConnect_Click(object sender, EventArgs e)
        {
            //TODO
            //string _ip = "192.168.1.11";
            //int ret = PMC_OpenEth(_ip, ref _handle);
            //if (ret == 0)
            //{
            //    isConnect = true;
            //    MessageBox.Show("连接成功");
            //    getPos();

            //}
        }

        private void Btnstop_Click(object sender, EventArgs e)
        {
            if (axisFlag)
            {
                foreach (var item in axisList)
                {
                    PMC_Rapidstop(_handle, 3);
                }
                Data._pause = false;
            }
        }

        private void btn_pause_Click(object sender, EventArgs e)
        {
            if (!Data._pause)
            {
                PMC_MovePause(Data.handle, 0, 0);
                btn_pause.Text = "恢 复";
                Data._pause = true;
            }
            else
            {
                PMC_MoveResume(Data.handle, 0);
                btn_pause.Text = "暂 停";
                Data._pause = false;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Data._pause) Btnstart.Enabled = false;            
        }
    }


}






